package com.wxx.controller.bg;

import com.wxx.common.Const;
import com.wxx.model.DicRole;
import com.wxx.model.DicUser;
import com.wxx.service.bg.DicRoleService;
import com.wxx.service.bg.DicUserService;
import com.wxx.util.*;
import com.wxx.vo.DicAclVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bg/dicUser/")
public class DicUserController {

    @Autowired
    private DicUserService dicUserService;
    @Autowired
    private DicRoleService dicRoleService;


    /**
     * 跳转到编辑用户页面
     * @return
     */
    @RequestMapping(value = "toEdit",method = RequestMethod.GET)
    public String goEdit(Model model, String uid){
        Map map = dicUserService.goEdit(uid);
        model.addAttribute("model",(DicUser)map.get("dicUser"));
        model.addAttribute("list",(List<DicRole>)map.get("excludeAdmin"));
        model.addAttribute("url", Const.URL);
        return "dicUser/edit";
    }

    /**
     * 编辑用户
     * @return
     */
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    @ResponseBody
    public Object edit(Model model, DicUser dicUser){
        Map map = dicUserService.edit(dicUser);
        return map;
    }

    /**
     * 删除用户
     * @return
     */
    @RequestMapping(value = "del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(Model model, String uid){
        Map map = dicUserService.del(uid);
        return map;
    }

    /**
     * 跳转到添加用户页面
     * @return
     */
    @RequestMapping(value = "toAdd",method = RequestMethod.GET)
    public String toAdd(Model model){
        //查询角色列表,只能添加除了超级管理员之外的角色
        List<DicRole> dicRoleList = dicRoleService.findDicRoleListByExcludeAdmin();

        model.addAttribute("list",dicRoleList);
        return "dicUser/add";
    }

    /**
     * 添加用户
     * @return
     */
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public Object add(Model model, DicUser dicUser){
        Map map = dicUserService.add(dicUser);
        return map;
    }

    /**
     * 跳转到后台用户列表页面
     * @return
     */
    @RequestMapping(value = "toDicUserList",method = RequestMethod.GET)
    public String toDicUserList(Model model){
        model.addAttribute("url", PropertiesUtil.getProperty("URL"));
        return "dicUser/list";
    }

    /**
     * 查询管理员列表
     * @return
     */
    @RequestMapping(value = "findDicUserList",method = RequestMethod.GET)
    @ResponseBody
    public Object findDicUserList(String roleName,Integer page,Integer limit){
        List<DicUser> dicUserList = dicUserService.findDicUserList(roleName,page,limit);
        return JsonData.returnLayuiMap(dicUserList, dicUserList.size());
    }


    /**
     * 登录
     * @param model
     * @param loginName
     * @param loginPwd
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(HttpServletResponse response, HttpSession session, ModelMap model, String loginName, String loginPwd){

        Map map = dicUserService.login(loginName,AppUtil.md5Salt(loginPwd));
        if (map.get("code").equals(Const.SUCCESS_CODE)){
            //登录成功 跳转到详情页面
            //查询该用户的权限和所有权限放到缓存中
            List<DicAclVo> aclVos = (List<DicAclVo>) map.get("aclVos");
            model.addAttribute("aclVos",aclVos);

            DicUser dicUser = (DicUser) map.get("user");
            DicRole dicRole = (DicRole) map.get("dicRole");
            model.addAttribute("user",dicUser);
            model.addAttribute("dicRole",dicRole);
            model.addAttribute("url", PropertiesUtil.getProperty("URL"));
            //全部权限的url
            List<String> allURIByRoleId = (List<String>) map.get("allURIByRoleId");

            //将用户信息存入redis
            //将登录信息放入到cookie里
            CookieUtil.writeLoginToken(response,session.getId());
            RedisPoolUtil.setEx(session.getId(), JsonUtil.obj2String(dicUser),Const.RedisCacheExtime.REDIS_SESSION_EXTIME);
            RedisPoolUtil.set(session.getId()+Const.CURRENT_URIBYROLEID, JsonUtil.obj2String(allURIByRoleId));

            return "home";
        }else{
            //登录失败 跳转到登录页面
            model.addAttribute("msg",map.get("msg"));
            return "login";
        }

    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping(value = "exit",method = RequestMethod.GET)
    public String exit(HttpServletRequest request, HttpServletResponse response){
        String sessionId = CookieUtil.readLoginToken(request);
        CookieUtil.delLoginToken(request,response);

        /*退出将redis的user和权限信息清除*/
        RedisPoolUtil.del(sessionId);
        RedisPoolUtil.del(sessionId+Const.CURRENT_URIBYROLEID);
        return "login";
    }
}
